package com.hdjk.view.fragment.main;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.hdjk.view.R;
import com.hdjk.view.activity.investment.ZHDRiskTestActivity;
import com.hdjk.view.activity.investment.ZHDRiskTestReturnActivity;
import com.hdjk.view.activity.main.LoginActivity;
import com.hdjk.view.callback.CallBackListener;
import com.hdjk.view.entity.UserEntity;
import com.hdjk.view.network.PersonalNetWork;
import com.hdjk.view.util.UserTokenUtil;
import com.lst.projectlib.base.BaseFram;
import com.lst.projectlib.entity.Msg;
import com.lst.projectlib.util.DensityUtil;

/**
 * 智投Fragment
 * Created by sxy on 2017/11/1.
 */

public class InvestmentFragment extends BaseFram implements View.OnClickListener {
    private boolean isCreate = false;
    // Content View Elements
    private ImageView iv_top1, iv_top2;
    private Button btn_start;
    private UserEntity mUser;
    // End Of Content View Elements

    private void bindViews(View view) {
        iv_top1 = (ImageView) view.findViewById(R.id.iv_top1);
        iv_top2 = (ImageView) view.findViewById(R.id.iv_top2);
        btn_start = (Button) view.findViewById(R.id.btn_start);
    }

    @Override
    protected View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_investment, container, false);
        bindViews(view);
        btn_start.setOnClickListener(this);
        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        ViewGroup.LayoutParams layoutParams = iv_top1.getLayoutParams();
        layoutParams.width = dm.widthPixels;
        layoutParams.height = (int) ((float) (dm.widthPixels) / 1080 * 571);
        iv_top1.setLayoutParams(layoutParams);
        LinearLayout.LayoutParams layoutParams2 = (LinearLayout.LayoutParams) iv_top2.getLayoutParams();
        layoutParams2.width = dm.widthPixels - DensityUtil.dip2px(getContext(), 20);
        layoutParams2.height = (int) ((float) (dm.widthPixels - DensityUtil.dip2px(getContext(), 20)) / 1010 * 2921);
        iv_top2.setLayoutParams(layoutParams2);
        isCreate = true;
        return view;
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_start:
                Intent intent;
                if (UserTokenUtil.isLogin(getContext())) {
                    mDialog.show();
                    PersonalNetWork.getUserInfo(UserTokenUtil.getToken(getContext()), new CallBackListener() {
                        @Override
                        public void onCallBackCompleted(Msg msg) {
                            mDialog.cancel();
                            Intent intent1;
                            if (msg != null && msg.getSuccess() && msg.getData() != null && msg.getData() instanceof UserEntity) {
                                mUser = (UserEntity) msg.getData();
                                if (mUser == null || TextUtils.isEmpty(mUser.getFirstDesc()) || "null".equals(mUser.getFirstDesc())) {
                                    intent1 = new Intent(getContext(), ZHDRiskTestActivity.class);
                                    startActivity(intent1);
                                } else {
                                    intent1 = new Intent(getContext(), ZHDRiskTestReturnActivity.class);
                                    intent1.putExtra("type", mUser.getFirstDesc());
                                    startActivity(intent1);
                                }
                            }else {
                                intent1 = new Intent(getContext(), ZHDRiskTestActivity.class);
                                startActivity(intent1);
                            }
                        }

                        @Override
                        public void onCallBackError(Msg msg) {
                            mDialog.cancel();
                        }
                    });

                } else {
                    intent = new Intent(getContext(), LoginActivity.class);
                    startActivityForResult(intent, 4);
                }
                break;
        }
    }
}
